﻿
<?php
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){

      if(isset($_POST['SendForm'])) {
      
         $action_nom = NULL; // valeur du champ nom 
      	$action_type_public = NULL; // valeur du champ public visé
      	$action_type_action = NULL; // valeur du champ type action visé
      	$action_date_debut = ""; // valeur du champ date de début
      	$action_date_fin = ""; // valeur du champ date de fin
      	$action_theme = ""; // valeur du champ thématique
      	$action_com = ""; // valeur du champ commentaire
         $statut = array();
         $erreur = false;
   		
   		if(isset($_POST['nom']) && $_POST['nom'] != "" && isset($_POST['public']) && $_POST['public'] != ""){
            
            $action_nom = htmlentities($_POST['nom'], ENT_NOQUOTES, "UTF-8");
            $action_type_public = htmlentities($_POST['public'], ENT_NOQUOTES, "UTF-8");
            $action_type_action = htmlentities($_POST['typeAction'], ENT_NOQUOTES, "UTF-8");
            
            if ($_POST['debutJour'] !="" && $_POST['debutMois'] !="" && $_POST['debutAnnee'] !=""){
               if (checkDate($_POST['debutMois'],$_POST['debutJour'],$_POST['debutAnnee'])){
                  $action_date_debut = $_POST['debutAnnee']."-".$_POST['debutMois']."-".$_POST['debutJour'];
               } else {
                  $statut[] = "La date de début n'est pas valide.";
                  $erreur = true;
               }
            } else if (!($_POST['debutJour']=="" && $_POST['debutMois']=="" && $_POST['debutAnnee']=="")) {
            
               $statut[] = "La date de début n'est pas valide.";
               $erreur = true;
            
            }
            
            if ($_POST['finJour']!="" && $_POST['finMois']!="" && $_POST['finAnnee']!=""){
               if (checkDate($_POST['finMois'],$_POST['finJour'],$_POST['finAnnee'])){
                  $action_date_fin = $_POST['finAnnee']."-".$_POST['finMois']."-".$_POST['finJour'];
               } else {
                  $statut[] = "La date de fin n'est pas valide.";
                  $erreur = true;
               }
            } else if (!($_POST['finJour']=="" && $_POST['finMois']=="" && $_POST['finAnnee']=="")) {
            
               $statut[] = "La date de fin n'est pas valide.";
               $erreur = true;
            
            }
            
            echo $action_date_debut." > ".$action_date_fin."<br />";
            
            if ($action_date_debut != "" && $action_date_fin != "" && $_POST['debutAnnee'].$_POST['debutMois'].$_POST['debutJour'] > $_POST['finAnnee'].$_POST['finMois'].$_POST['finJour']) {
               
               $statut[] = "La date de fin précède celle de début.";
               $erreur = true;
               
            }
            
            if (isset($_POST['com']) && $_POST['com'] != ""){
               $action_com = nl2br(htmlentities($_POST['com'], ENT_NOQUOTES, "UTF-8"));
            }
            
            if (!$erreur) {
            
               $query_create_action = "INSERT INTO action (NOMACTION,DATEDEBUT,DATEFIN,COMMENTAIRE) VALUES (".mysqlString($action_nom).",'$action_date_debut','$action_date_fin',".mysqlString($action_com).")";
               $result_create_action = mysql_query($query_create_action, $connexion) or logError("CREATION ACTION-".$query_create_action."-".mysql_error());
               
               $idAction = mysql_insert_id($connexion);
               
               $query_recup_typepublic = "SELECT CODETYPEPUBLIC as id,NOMTYPEPUBLIC as nom FROM type_public";
               $result_recup_typepublic = mysql_query($query_recup_typepublic, $connexion) or logError("RECUP TYPE PUBLIC-".$query_recup_typepublic."-".mysql_error());
               
               $cpt=0;
               
               $idTypePublic = "";
               
               $tab_list_typepublic = array(array());
               
               while($tab = mysql_fetch_assoc($result_recup_typepublic)){
                  $tab_list_typepublic[0][$cpt] = $tab['id'];
                  $tab_list_typepublic[1][$cpt] = $tab['nom'];
                  $cpt++;
               }
               
               mysql_free_result($result_recup_typepublic);
               
               if ($cpt > 0 && !valueIsInTab($tab_list_typepublic[1],$action_type_public)){
                  
                  $query_create_type_public = "INSERT INTO type_public (NOMTYPEPUBLIC) VALUES (".mysqlString($action_type_public).")";
                  $result_create_type_public = mysql_query($query_create_type_public, $connexion) or logError("CREATION TYPE_PUBLIC-".$query_create_type_public."-".mysql_error());
                  
                  $idTypePublic = mysql_insert_id($connexion);
                  
               } else {
                  $cpt2 = 0;
                  while ($cpt2<=$cpt && $idTypePublic == "") {
                     if($tab_list_typepublic[1][$cpt2] == $action_type_public){
                     
                        $idTypePublic = $tab_list_typepublic[0][$cpt2];
                     
                     }
                     $cpt2++;
                  }
               }
               
               $query_recup_typeaction = "SELECT CODETYPEACTION as id,NOMTYPEACTION as nom FROM type_action";
               $result_recup_typeaction = mysql_query($query_recup_typeaction, $connexion) or logError("RECUP TYPE action-".$query_recup_typeaction."-".mysql_error());
               
               $cpt=0;
               
               $idTypeaction = "";
               
               $tab_list_typeaction = array(array());
               
               while($tab = mysql_fetch_assoc($result_recup_typeaction)){
                  $tab_list_typeaction[0][$cpt] = $tab['id'];
                  $tab_list_typeaction[1][$cpt] = $tab['nom'];
                  $cpt++;
               }
               
               mysql_free_result($result_recup_typeaction);
               
               if ($cpt > 0 && !valueIsInTab($tab_list_typeaction[1],$action_type_action)){
                  
                  $query_create_type_action = "INSERT INTO type_action (NOMTYPEACTION) VALUES (".mysqlString($action_type_action).")";
                  $result_create_type_action = mysql_query($query_create_type_action, $connexion) or logError("CREATION TYPE_ACTION-".$query_create_type_action."-".mysql_error());
                  
                  $idTypeaction = mysql_insert_id($connexion);
                  
                  $query_create_est_type_action = "INSERT INTO est_de_type_action (CODEACTION,CODETYPEACTION) VALUES ($idAction, $idTypeAction)";
                  $result_create_est_type_action = mysql_query($query_create_est_type_action, $connexion) or logError("CREATION TYPE_ACTION-".$query_create_est_type_action."-".mysql_error());
                  
               } else {
                  $cpt2 = 0;
                  while ($cpt2<$cpt && $idTypeaction == "") {
                     if($tab_list_typeaction[1][$cpt2] == $action_type_action){
                     
                        $idTypeaction = $tab_list_typeaction[0][$cpt2];
                     
                     }
                     $cpt2++;
                  }
               }
               
               $query_assoc_touche_public = "INSERT INTO touche_public (CODEACTION,CODETYPEPUBLIC) VALUES ($idAction,$idTypePublic)";
               $result_assoc_touche_public = mysql_query($query_assoc_touche_public, $connexion) or logError("CREATION ASSOC TOUCHE PUBLIC-".$query_assoc_touche_public."-".mysql_error());
               
               if (isset($_POST['theme']) && $_POST['theme'] != "") {
                  
                  $action_theme = strtolower(htmlentities($_POST['theme'], ENT_NOQUOTES, "UTF-8"));
                  
                  $query_recup_theme = "SELECT CODETHEMATIQUE as id,NOMTHEMATIQUE as nom FROM thematique";
                  $result_recup_theme = mysql_query($query_recup_theme, $connexion) or logError("RECUP THEMATIQUE-".$query_recup_theme."-".mysql_error());
                  
                  $cpt=0;
                  
                  $idTheme = "";
                  
                  while($tab = mysql_fetch_assoc($result_recup_theme)){
                     $tab_list_theme[0][$cpt] = $tab['id'];
                     $tab_list_theme[1][$cpt] = strtolower($tab['nom']);
                     $cpt++;
                  }
                  
                  mysql_free_result($result_recup_theme);
                  
                  if (!valueIsInTab($tab_list_theme[1],$action_theme)){
                     
                     $query_create_theme = "INSERT INTO thematique (NOMTHEMATIQUE) VALUES (".mysqlString($action_theme).")";
                     $result_create_theme = mysql_query($query_create_theme, $connexion) or logError("CREATION THEME-".$query_create_theme."-".mysql_error());
                     
                     $idTheme = mysql_insert_id($connexion);
                     
                  } else {
                     $cpt2 = 0;
                     while ($cpt2<$cpt && $idTheme == "") {
                        if($tab_list_theme[1][$cpt2] == $action_theme){
                        
                           $idTheme = $tab_list_theme[0][$cpt2];
                        
                        }
                        $cpt2++;
                     }
                  }
                  
                  $query_assoc_action_theme = "INSERT INTO action_a_pour_theme (CODEACTION,CODETHEMATIQUE) VALUES ($idAction,$idTheme)";
                  $result_assoc_action_theme = mysql_query($query_assoc_action_theme, $connexion) or logError("CREATION ASSOC ACTION THEME-".$query_assoc_action_theme."-".mysql_error());
                  
               }
               
               $statut[] = "L'action a été ajoutée - redirection dans 2 sec...";
               
            }
            
   		} else {
            $statut[] = "Tous les champs obligatoires n'ont pas été remplis.";
            $erreur = true;
         }
   		
         if ($erreur) {
            afficherStatut($statut, 3);
            redirection("gestion/action_ajoute",2);
         } else {
            afficherStatut($statut, 1);
            redirection("gestion/action_gere",2);
         }
         
      } else {
         
         
         boutonRetour("index.php?p=gestion/action_gere"); // appel à la fonction boutonRetour() définie dans fonctions.php
         
   ?>

   <div class="addAction">
      
      <h2>Ajout d'une Action</h2>
      
      <form action="" method="post"> 
         
         <div class="subtitle43"> Informations principales </div>
         
         <div class="actionTitle">        
            <span class="actionLabel1">
               Nom * :
            </span>
            <span class="actionInput1"> 
               <input type="text" name="nom" autocomplete="off" value="" />
            </span>
         </div>
            
         <div class="actionTitle">        
            <span class="actionLabel1">
               Type d'action * :
            </span>
            <span class="actionInput1"> 
               <input type="text" id="typeAction" name="typeAction" value="" autocomplete="off" onkeyup="afficherListeItems('typeAction',true,'typeaction.php');" />
               <?php afficheAutoCompl("typeAction"); ?>
            </span>
         </div> 
         
         <div class="actionTitle">        
            <span class="actionLabel1">
               Public touché * :
            </span>
            <span class="actionInput1"> 
               <input type="text" id="public" name="public" value="" autocomplete="off" onkeyup="afficherListeItems('public',true,'typepublic.php');" />
               <?php afficheAutoCompl("public"); ?>
            </span>
         </div>
         
         <div class="actionTitle">        
            <span class="actionLabel1">
               Date de début :
            </span>
            <span class="actionInput1"> 
               <select name="debutJour">
                  <option>
                  <?php
                     $cpti = 01;
                     while ($cpti<32){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
               / <select name="debutMois">
                  <option>
                  <?php
                     $cpti = 01;
                     while ($cpti<13){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
               / <select name="debutAnnee">
                  <option>
                  <?php
                     $cpti = date('Y')-5;
                     while ($cpti<(date('Y')+5)){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
            </span>
         </div>
         
         <div class="actionTitle">        
            <span class="actionLabel1">
               Date de fin :
            </span>
            <span class="actionInput1"> 
               <select name="finJour">
                  <option>
                  <?php
                     $cpti = 01;
                     while ($cpti<32){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
               / <select name="finMois">
                  <option>
                  <?php
                     $cpti = 01;
                     while ($cpti<13){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
               / <select name="finAnnee">
                  <option>
                  <?php
                     $cpti = date('Y')-5;
                     while ($cpti<date('Y')+5){
                        echo "<option> $cpti";
                        $cpti++;
                     }   
                  
                  ?>
               </select>
            </span>
         </div>
         
         <div class="actionTitle">        
            <span class="actionLabel1">
               Thématique :
            </span>
            <span class="actionInput1"> 
               <input type="text" id="theme" name="theme" value="" autocomplete="off" onkeyup="afficherListeItems('theme',true,'themes.php');" />
               <?php afficheAutoCompl("theme"); ?>
            </span>
         </div>
         
         <div class="subtitle43"> Plantes impliquées </div>
         
         <div class="actionTitle">        
            <div class="actionLabel1">
               Plantes :
            </div>
            <div class="actionInput1">
         <div style="float: left;">
				<b>Liste :</b><br />
				<select id="lst_ouvrages" name="lst_ouvrages[]" multiple="multiple" style="min-width: 175px;" ondblclick="transfererOptions('lst_ouvrages', 'ajt_reference');" size="6">
					<?php
						if($nb_references > 0){
							foreach($selectReferences as $ouvrage){
								if(!in_array($ouvrage['id'], $dv_reference)){
									echo "<option value=\"".$ouvrage['id']."\">".htmlentities(truncate($ouvrage['nom'], 30), ENT_NOQUOTES, "UTF-8")."</option>";
								}
							}
							unset($ouvrage);
						}else{
							echo "<option disabled=\"disabled\">Aucun ouvrage enregistré</option>";
						}
					?>
				</select>
			</div>
         
         <div style="float: left; margin-left: 10px; margin-right: 12px; padding-top: 40px;">
            <input type="button" value=">>" onclick="transfererOptions('lst_ouvrages', 'ajt_reference')" /><br />
				<input type="button" value="<<" onclick="transfererOptions('ajt_reference', 'lst_ouvrages')" />
			</div>
			<div style="float: right;">
				<b>Sélection :</b><br />
				<select id="ajt_reference" name="ajt_reference[]" multiple="multiple" style="min-width: 175px;" ondblclick="transfererOptions('ajt_reference', 'lst_ouvrages');" size="6">
					<?php
						if($nb_references > 0){
							foreach($selectReferences as $ouvrage){
								if(in_array($ouvrage['id'], $dv_reference)){
									echo "<option value=\"".$ouvrage['id']."\" selected=\"selected\">".htmlentities(truncate($ouvrage['nom'], 30), ENT_NOQUOTES, "UTF-8")."</option>";
								}
							}
							unset($ouvrage);
						}
					?>
				</select>
			</div>
			<div style="clear: both;">
				<input type="text" class="recherche" id="srch_ouvrage" style="vertical-align: top;" value="recherche..." size="10" onfocus="clearField('srch_ouvrage', 'recherche...');" onblur="resetField('srch_ouvrage', 'recherche...');" onkeyup="trierSelect('lst_ouvrages', 'srch_ouvrage');" />
				<br />
				<input type="button" value="Ajouter un document" onclick="popup('popup.php?p=gestion/reference_ajoute_popup&amp;maj=lst_ouvrages[]&amp;sel=1', 600, 520);" />
			</div>
         </div>
         </div>
         <div class="actionTitle">        
            <span class="actionLabel1">
               Commentaire :
            </span>
            <span class="actionInput1"> 
               <textarea name="com" autocomplete="off" value="" /></textarea>
            </span>
         </div>
         
         <div class="actionTitle2">
            
            <span class="actionInput2"> 
               <input type="submit" name="SendForm" value="Ajouter">
            </span>
         </div>
         
         
      </form>
   </div>
           


<div class="information">
	<div class="titre">Informations</div>
	<h4>rien :</h4>
	<p>pas d'informations.</p>
</div>

<?php
      }
	}
	else{
		if(isset($_SESSION)){
			if(!isset($_SESSION['user_id'])){ // si l'utilisateur n'est pas connecté
				include("pages/connexion.php");
			}else{ // si l'utilisateur est connecté et qu'il n'a pas accès à la page, c'est qu'il n'a pas le droit d'utilisation nécessaire
				include("pages/401.htm");
			}
		}
	}
?>